package fr.gaulupeau.apps.Poche.tts.engine.local;

import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;

import java.util.Locale;

import fr.gaulupeau.apps.Poche.tts.TtsConfig;
import fr.gaulupeau.apps.Poche.tts.engine.AbstractTtsEngine;

public class LocalTtsEngine extends AbstractTtsEngine {

    private  TextToSpeech tts; // TODO: fix: access is not properly synchronized

    public static final String NAME="local";

    private String ttsVoice;

    private String engineName;


    public LocalTtsEngine(String engineName, Context context, TtsConfig settings, TextToSpeech.OnInitListener initListener) {
        this.engineName = engineName;
        isTtsInitialized = onCreate(context,settings,initListener);
    }

    @Override
    public boolean onCreate(Context context, TtsConfig settings, TextToSpeech.OnInitListener listener) {
        super.onCreate(context,settings,listener);
        if(engineName==null||engineName.isEmpty()||NAME.equalsIgnoreCase(engineName))
            tts = new TextToSpeech(context, listener);
        else
            tts = new TextToSpeech(context, listener,engineName);
        onConfigChanged();
       return true;
    }

    @Override
    public void shutdown() {
        tts.shutdown();
    }

    @Override
    public String engineName() {
        return NAME;
    }

    @Override
    public void speak(CharSequence text, int queueMode, String utteranceId) {
        tts.speak(text, queueMode, null, utteranceId);
    }

    @Override
    public void stop() {
        tts.stop();
    }

    @Override
    public void onConfigChanged() {
        ttsVoice = settings.getTtsVoice();
        if(ttsVoice!=null)
            tts.setLanguage(convertVoiceNameToLocale(ttsVoice));
        tts.setSpeechRate(settings.getTtsSpeed());
        tts.setPitch(settings.getTtsPitch());
    }

    @Override
    public void setSpeed(float speed) {
        tts.setSpeechRate(speed);
    }

    @Override
    public void setPitch(float pitch) {
        tts.setPitch(pitch);
    }

    @Override
    public void setLanguage(Locale convertVoiceNameToLocale) {
        tts.setLanguage(convertVoiceNameToLocale);
    }

    @Override
    public void setSpeechRate(float speedRate) {
        tts.setSpeechRate(speedRate);
    }

    @Override
    public void setOnUtteranceProgressListener(UtteranceProgressListener utteranceProgressListener) {
        tts.setOnUtteranceProgressListener(utteranceProgressListener);
    }

    private Locale convertVoiceNameToLocale(String voiceName) {
        String language;
        String country = "";
        String variant = "";
        int separatorIndex = voiceName.indexOf("-");
        if (separatorIndex >= 0) {
            language = voiceName.substring(0, separatorIndex);
            country = voiceName.substring(separatorIndex + 1);
            separatorIndex = country.indexOf("-");
            if (separatorIndex >= 0) {
                variant = country.substring(separatorIndex + 1);
                country = country.substring(0, separatorIndex);
            }
        } else {
            language = voiceName;
        }
        return new Locale(language, country, variant);
    }
}
